package k8.buffer;

import java.nio.ByteBuffer;

public final class Slice
{

	private Buffer buffer;
	private ByteBuffer slice;

	/** Creates an instance of Slice */
	public Slice(Buffer buffer, ByteBuffer slice)
	{
		this.buffer = buffer;
		this.slice = slice;
	}

	/**
	 * Populates the slice with nine values.
	 * 
	 * @param f1
	 *            value for position 0 in slice
	 * @param f2
	 *            value for position 4 in slice
	 * @param f3
	 *            value for position 8 in slice
	 * @param f4
	 *            value for position 12 in slice
	 * @param f5
	 *            value for position 16 in slice
	 * @param f6
	 *            value for position 20 in slice
	 * @param f7
	 *            value for position 24 in slice
	 * @param f8
	 *            value for position 28 in slice
	 * @param f9
	 *            value for position 32 in slice
	 */
	public void put(float f1, float f2, float f3, float f4, float f5, float f6,
			float f7, float f8, float f9)
	{
		slice.putFloat(0, f1);
		slice.putFloat(4, f2);
		slice.putFloat(8, f3);
		slice.putFloat(12, f4);
		slice.putFloat(16, f5);
		slice.putFloat(20, f6);
		slice.putFloat(24, f7);
		slice.putFloat(28, f8);
		slice.putFloat(32, f9);
		buffer.flush();
	}

	/**
	 * Populates the slice with six floats.
	 * 
	 * @param f1
	 *            value for position 0 in slice
	 * @param f2
	 *            value for position 4 in slice
	 * @param f3
	 *            value for position 8 in slice
	 * @param f4
	 *            value for position 12 in slice
	 * @param f5
	 *            value for position 16 in slice
	 * @param f6
	 *            value for position 20 in slice
	 */
	public void put(float f1, float f2, float f3, float f4, float f5, float f6)
	{
		slice.putFloat(0, f1);
		slice.putFloat(4, f2);
		slice.putFloat(8, f3);
		slice.putFloat(12, f4);
		slice.putFloat(16, f5);
		slice.putFloat(20, f6);
		buffer.flush();
	}

	/** Clears the slice to zero values */
	public void clear()
	{
		slice.rewind();
		while (slice.hasRemaining())
			slice.putFloat(0);
		buffer.flush();
	}

	/** Gets the underlying ByteBuffer */
	public ByteBuffer get()
	{
		return slice;
	}

	/** Puts the contents of slice into this Slices slice */
	public void put(ByteBuffer slice)
	{
		this.slice.put(slice);
	}

	/** Discards this Slice */
	public void discard()
	{
		buffer.disgard(this);
	}

}
